package com.tripcube.common.redis.key;

import org.springframework.beans.factory.annotation.Value;

public class RedisKeyBuilder {
    @Value("${spring.application.name}")
    private String applicationName;

    private static final String SPILT_ITEM = ":";

    private String getSpiltItem(){
        return SPILT_ITEM;
    }

    private String getPrefix(){
        return applicationName + SPILT_ITEM;
    }

    /**
     * 构建基础key
     * @param identifiers 标识符列表
     * @return 完整的key
     */
    protected String buildKey(Object... identifiers) {
        StringBuilder keyBuilder = new StringBuilder();
        keyBuilder.append(getPrefix());

        for (Object identifier : identifiers) {
            keyBuilder.append(identifier).append(getSpiltItem());
        }

        if (!keyBuilder.isEmpty() && keyBuilder.charAt(keyBuilder.length() - 1) == getSpiltItem().charAt(0)) {
            keyBuilder.deleteCharAt(keyBuilder.length() - 1);
        }

        return keyBuilder.toString();
    }
}
